<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <!-- sessionStorage -->
    <input type="text" name="" id="inp">
    <input type="button" value="设置数据" id="setData">
    <input type="button" value="获取数据" id="getData">
    <input type="button" value="删除数据" id="removeData">
</body>
<script>
    function AutoJson(o){
        if( o.__proto__.constructor.name === 'String'){
            console.log('string 转 Json');
            return JSON.parse(o)
            
        }else if (o.__proto__.constructor.name === 'Object'){
            console.log('JSON 转 string');
            return JSON.stringify(o)
        }else {
            console.log('错误的类型：'+ o)
        }
    };
    document.querySelector("#setData").addEventListener('click',function(){
        var name = new Object({
            name:'小6',
            type:'人类',
            none:{
                "1":1,
                "2":2
            }
        });
        var sname = AutoJson(name);
        console.log(name );
        var ses = document.querySelector('#inp');
        console.log(name);
        window.sessionStorage.setItem("usermessage",sname);
        
    })
    document.querySelector("#getData").addEventListener('click',function(){
        var a = window.sessionStorage.getItem("usermessage");
        a = AutoJson(a);
        console.log(a);
    })
    document.querySelector("#removeData").addEventListener('click',function(){
        var a = window.sessionStorage.removeItem("usermessage");
        
    })
</script>
</html>